﻿// <copyright file="ContentKey.cs" company="Maierhofer Software">
// Copyright (c) 2010 by Maierhofer Software. All rights reserved.
// </copyright>
namespace WAF.Server.Ipc
{
    using System;
    using System.Runtime.Serialization;

    /// <summary>
    /// Identifies the content during web servr and app server requests and responses
    /// </summary>
    [DataContract]
    public class ContentKey : IEquatable<ContentKey>
    {
        /// <summary>
        /// The web server ID. Every web server process has its own ID
        /// </summary>
        [DataMember]
        public Guid WebServerID;

        /// <summary>
        /// The number of the request. Every request gets its own number. 
        /// </summary>
        [DataMember]
        public long RequestNumber;

        /// <summary>
        /// Initializes a new instance of the <see cref="ContentKey"/> class.
        /// </summary>
        /// <param name="webServerID">The web server ID.</param>
        /// <param name="requestNumber">The request number.</param>
        public ContentKey(Guid webServerID, long requestNumber)
        {
            this.WebServerID = webServerID;
            this.RequestNumber = requestNumber;
        }

        /// <summary>
        /// Returns a hash code for this instance.
        /// </summary>
        /// <returns>
        /// A hash code for this instance, suitable for use in hashing algorithms and data structures like a hash table.
        /// </returns>
        public override int GetHashCode()
        {
            return this.WebServerID.GetHashCode() ^ this.RequestNumber.GetHashCode();
        }

        /// <summary>
        /// Checks if this and the specified obj are equal.  
        /// </summary>
        /// <param name="obj">The object to compare with</param>
        /// <returns>true if this and obj are equal, otherwhise false.</returns>
        public bool Equals(ContentKey obj)
        {
            return this.WebServerID.Equals(obj.WebServerID) && obj.RequestNumber == this.RequestNumber;
        }

        public override string ToString()
        {
            return "ContentKey: " + WebServerID + " [" + RequestNumber + "]";
        }
    }
}
